#include "Account.hh"
#include "Transaction.hh"
#include "Utility.hh"
#include <iomanip>

void Account::deposit(double amount, const std::string& note) {
    if (amount > 0) {
        balance += amount;
        Transaction transaction(amount, "Deposit", note);
        recordTransaction(transaction);
    } else {
        std::cout << "Invalid deposit amount.\n";
    }
}

void Account::withdraw(double amount, const std::string& note) {
    if (amount > 0 && balance >= amount) {
        balance -= amount;
        Transaction transaction(-amount, "Withdrawal", note);
        recordTransaction(transaction);
    } else {
        std::cout << "Insufficient funds or invalid withdrawal amount.\n";
    }
}

void Account::transfer(Account* to, double amount, const std::string& note) {
    if (amount > 0 && balance >= amount) {
        balance -= amount;
        to->deposit(amount, note);
        Transaction transaction(-amount, "Transfer", note);
        recordTransaction(transaction);
    } else {
        std::cout << "Insufficient funds or invalid transfer amount.\n";
    }
}

void Account::recordTransaction(const Transaction& transaction) {
    transactions.push_back(transaction);
}

const std::string& Account::getAccountID() const {
    return accountID;
}

double Account::getBalance() const {
    return balance;
}

const std::string& Account::getOwner() const {
    return owner;
}

const std::vector<Transaction>& Account::getTransactions() const {
    return transactions;
}

Account::Account(const std::string& id, double initialBalance, const std::string& owner)
    : accountID(id), balance(initialBalance), owner(owner) {}

void Account::accessAccount() {
    std::cout << "Account ID: " << accountID << "\n";
    std::cout << "Owner: " << owner << "\n";
    std::cout << "Balance: $" << balance << "\n";
    std::cout << "Transactions:\n";
    for (const auto& transaction : transactions) {
        std::cout << "  " << ": " << transaction.getAmount() << " " << transaction.getType() << " - " << transaction.getNote() << "\n";}
    }